Next | Prev | Up | Top | Contents | Index

Comparing the Timestamps

The two timestamp sources can be compared on several different attributes, listed in Table 5-4.

Comparison of Timestamp Functions
TimestampOverheadNominal PrecisionAccuracyDrift
gettimeofday()

clock_gettime()

System call (100s of instructions)1 microsecond

1 nanosecond

+0, -10 milliseconds1 part in 10,000 short-term; corrected to negligible amount over long periods
cycle countermicroseconds 21 nanoseconds (Challenge)instruction cycle time1 part in 10,000, varying

Because gettimeofday() is synchronized to a time standard, you must use it when you want to record the actual time of an event, and when times recorded in one machine will be compared to times recorded in another. You should use it to measure durations of minutes and longer; in those cases its possible error of up to -10 milliseconds becomes less important than its resistance to long-term drift.

Because the cycle counter can be sampled with negligible overhead and has very high precision, you should use it whenever you want to measure durations of seconds or less, and whenever you simply want a source of unduplicated, monotonically-increasing, unsigned numbers to provide unique key values.


Next | Prev | Up | Top | Contents | Index